package redis

import (
	"encoding/json"
	redis2 "github.com/gomodule/redigo/redis"
)

type hash struct {
	conn redis2.Conn
}

func (h *hash) Set(table string, key string, value interface{}) {
	data, _ := json.Marshal(value)
	h.conn.Send("hset", []interface{}{table, key, data}...)
	defer h.conn.Close()
}

func (h *hash) Exists(table string, key string) bool {
	req, _ := h.conn.Do("hexists", []interface{}{table, key}...)
	if req.(int64) == 1 {
		return true
	}
	return false
}

func (h *hash) Get(table string, key string, data any) {
	req, _ := h.conn.Do("hget", []interface{}{table, key}...)
	json.Unmarshal(req.([]byte), &data)
}
